# Maintainer: David Runge <dvzrv@archlinux.org>
# Contributor: Johannes Maibaum <jmaibaum at gmail dot com>
# Contributor: Christopher Arndt <aur -at- chrisarndt -dot- de>

_name=setBfree
pkgbase=setbfree
pkgname=(setbfree setbfree-common setbfree-lv2 setbfree-standalone)
pkgver=0.8.12
pkgrel=1
pkgdesc="DSP tonewheel organ"
arch=(loong64 x86_64)
url="https://setbfree.org"
license=(GPL2)
makedepends=(
  cairo
  ftgl
  gendesk
  glib2
  jack
  libglvnd
  liblo
  libx11
  lv2
  pango
  ttf-dejavu
)
checkdepends=(lv2lint)
source=(
  https://github.com/pantherb/$_name/archive/v$pkgver/$_name-$pkgver.tar.gz
  $pkgbase-0.8.11-gl.patch
)
sha512sums=('46d9290d8cf6de636e292854933393605d4ea61f924d453b046ef6dcb3ca3a683bc2be4a1b81e95a9802c3ed0d9ffe318faa5eb7034926a52b3086d89ca3fc31'
            'a381781e1a2e9d71267f156ff0c4879ff7b3c73da1be7a7e437fda5208154330914911e46b16c1e0290ded9ccc339b402dbc0ad32766e13d777afc95ce5c9133')
b2sums=('4979081d95eb2e0d243e9fc1b836a200b3c8dd638c7c7ffcfa4a3d671a02509861f33d29e3c350802d3d2c63cb0e58fdc63c50100763f6ca1d9a960b956c0f4d'
        '796907c3ae4398cfed56c4109325eeeaf853dd467fbbb386f67c8f6e61cd04bb6d341bbe97c16c21ae5ea770f9c6574a35bb4f9ab460b93ba54bf86eab7a24c9')

_pick() {
  local p="$1" f d; shift
  for f; do
    d="$srcdir/$p/${f#$pkgdir/}"
    mkdir -p "$(dirname "$d")"
    mv "$f" "$d"
    rmdir -p --ignore-fail-on-non-empty "$(dirname "$f")"
  done
}

prepare() {
  # add missing library to link against: https://github.com/pantherb/setBfree/pull/84
  patch -d $_name-$pkgver -p1 -i ../$pkgbase-0.8.11-gl.patch

  gendesk -n \
          --pkgname "${pkgbase}" \
          --name "${_name}" \
          --exec "${_name}UI" \
          --icon "${_name}" \
          --genericname "${pkgdesc}" \
          --pkgdesc "${pkgdesc}" \
          --categories "AudioVideo;Audio"
  gendesk -n \
          --pkgname x42-whirl \
          --name x42-whirl \
          --genericname "Leslie speaker emulation" \
          --pkgdesc "Leslie speaker emulation as JACK client" \
          --categories "AudioVideo;Audio"
}

build() {
  export FONTFILE=/usr/share/fonts/TTF/DejaVuSans-Bold.ttf
  make PREFIX=/usr -C $_name-$pkgver
}

check() {
  lv2lint -Mpack -I $_name-$pkgver/b_synth "http://gareus.org/oss/lv2/b_synth"
  lv2lint -Mpack -I $_name-$pkgver/b_whirl "http://gareus.org/oss/lv2/b_whirl#simple"
  lv2lint -Mpack -I $_name-$pkgver/b_whirl "http://gareus.org/oss/lv2/b_whirl#extended"
}

package_setbfree() {
  depends=(
    "setbfree-lv2=$pkgver"
    "setbfree-standalone=$pkgver"
  )

  make PREFIX=/usr DESTDIR="$pkgdir" install -C $_name-$pkgver
  install -vDm 644 $_name-$pkgver/{AUTHORS,ChangeLog,README.md} -t "$pkgdir/usr/share/doc/$pkgname/"

  (
    cd "$pkgdir"
    _pick $pkgbase-common usr/share

    _pick $pkgbase-lv2 usr/lib/lv2

    _pick $pkgbase-standalone usr/bin
  )
}

package_setbfree-common() {
  pkgdesc+=" common files"
  mv -v $pkgname/* "$pkgdir"
}

package_setbfree-lv2() {
  pkgdesc+=" LV2 plugin"
  depends=(
    cairo
    ftgl
    glibc
    glib2 libgobject-2.0.so
    libglvnd
    libx11
    lv2-host
    pango libpango-1.0.so libpangocairo-1.0.so
    setbfree-common
  )
  groups=(
    lv2-plugins
    pro-audio
  )

  mv -v $pkgname/* "$pkgdir"
}

package_setbfree-standalone() {
  pkgdesc+=" JACK standalone"
  depends=(
    cairo
    ftgl
    glib2 libgobject-2.0.so
    glibc
    jack
    libglvnd
    liblo
    libx11
    pango libpango-1.0.so libpangocairo-1.0.so
    setbfree-common
  )
  groups=(
    pro-audio
  )

  mv -v $pkgname/* "$pkgdir"

  # xdg desktop files
  install -vDm 644 *.desktop -t "$pkgdir/usr/share/applications"
  # icons
  install -vDm 644 $_name-$pkgver/doc/{$_name,x42-whirl}.png -t "$pkgdir/usr/share/pixmaps/"
  # man
  install -vDm 644 $_name-$pkgver/doc/*.1 -t "$pkgdir/usr/share/man/man1/"
}